<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>第二题</title>
	</head>
	<body>
		<script type="text/javascript">
			//练习2：有已知以下某公司员工薪金信息，把每一位员工的salary薪金信息 加 2000，并返回新数组，注意：不影响原数组
			let persons = [
			    {
			        username:'张飞',
			        sex:'男',
			        salary:50000 
			    },
			    {
			        username:'关羽',
			        sex:'男',
			        salary:60000
			    }
			]
			function getType(data)
			{
				return Object.prototype.toString.call(data).slice(8,-1);
			}
			function toCopy(data)
			{
				if(getType(data) == "Array")
				{
					var reg = [];
				}
				else if(getType(data) == "Object")
				{
					var reg = {};
				}
				else
				{
					return data;
				}
				for(var key in data)
				{
					if(getType(data[key]) == "Array" || getType(data[key]) == "Object")
					{
						reg[key] = toCopy(data[key])
					}
					else
					{
					reg[key] = data[key];	
					}
					
				}
				return reg;
			}
		 var persons2 = [];
		 persons2 =  toCopy(persons);
		 for(var key in persons2)
		 {
			 persons2[key].salary += 2000;
		 }
		 console.log(persons);
		 console.log(persons2);
		</script>
	</body>
</html>
